package com.school.graduation.mine.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.school.graduation.R;
import com.school.graduation.common.base.BaseFragment;
import com.school.graduation.ui.activity.mainUi.MainActivity;
import com.school.graduation.util.ToastUtil;
import com.school.graduation.util.UserUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.Subscriber;

import static cn.bmob.v3.Bmob.getApplicationContext;

public class    MineFragment extends BaseFragment implements ImplMineFragment {
    @Bind(R.id.username_mine)
    TextView usernameMine;
    @Bind(R.id.description_mine)
    TextView descriptionMine;
    @Bind(R.id.head_img_mine)
    CircleImageView headImgMine;
    @Bind(R.id.head_mine)
    RelativeLayout headMine;
    @Bind(R.id.separate_01)
    ImageView separate01;
    @Bind(R.id.credit_mine)
    TextView creditMine;
    @Bind(R.id.creditHint_mine)
    TextView creditHintMine;
    @Bind(R.id.creditRL_mine)
    RelativeLayout creditRLMine;
    @Bind(R.id.creditRating_mine)
    TextView creditRatingMine;
    @Bind(R.id.creditRatingHint_mine)
    TextView creditRatingHintMine;
    @Bind(R.id.creditRatingRL_mine)
    RelativeLayout creditRatingRLMine;
    @Bind(R.id.creditLL_mine)
    LinearLayout creditLLMine;
    @Bind(R.id.infoRL_mine)
    RelativeLayout infoRLMine;
    @Bind(R.id.about_us)
    RelativeLayout aboutUs;
    @Bind(R.id.feedback)
    RelativeLayout feedback;
    @Bind(R.id.help)
    RelativeLayout help;
    @Bind(R.id.up)
    RelativeLayout up;
    @Bind(R.id.mine_released)
    RelativeLayout mMineReleased;

    @Override
    public int getFragmentView() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initData() {
        SharedPreferences sp = getApplicationContext().getSharedPreferences("user_cache", Context.MODE_MULTI_PROCESS |
                Activity.MODE_WORLD_READABLE);
        usernameMine.setText(sp.getString("name", "no_name_flag"));
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.head_mine, R.id.about_us, R.id.feedback, R.id.help, R.id.up, R.id.mine_released})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.head_mine:
                String heads[] = new String[]{"退出登录"};
                new MaterialDialog.Builder(getContext())
                        .items(heads)
                        .itemsCallback(new MaterialDialog.ListCallback() {
                            @Override
                            public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                                UserUtil.getCurrentUser(getContext()).logOut();
                                ToastUtil.makeShortToast(getContext(), "退出登录成功");
                                MainActivity activity = (MainActivity) getActivity();
                                activity.clickFirstItem();
                            }
                        }).build().show();
                break;
            case R.id.about_us:
                intent = new Intent(getContext(), AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.feedback:
                intent = new Intent(getContext(), FeedbackActivity.class);
                startActivity(intent);
                break;
            case R.id.help:
                intent = new Intent(getContext(), HelpActivity.class);
                startActivity(intent);
                break;
            case R.id.up:
                ToastUtil.makeShortToast(getContext(), "已是最新版");
                break;
            case R.id.mine_released:
                intent = new Intent(getContext(), MineReleasedActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
